use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Config {
    pub database_url: String,
    pub grpc_addr: String,
    pub nats_url: String,
}

impl Default for Config {
    fn default() -> Self {
        Self {
            database_url: "sqlite:///Users/xiaofeiwu/drafts/localbiz/reserv/data/reservations.db".to_string(),
            grpc_addr: "[::1]:50051".to_string(),
            nats_url: "nats://localhost:4222".to_string(),
        }
    }
}

impl Config {
    pub fn from_env() -> Result<Self, config::ConfigError> {
        let config = config::Config::builder()
            .set_default("database_url", "sqlite:///Users/xiaofeiwu/drafts/localbiz/reserv/data/reservations.db")?
            .set_default("grpc_addr", "[::1]:50051")?
            .set_default("nats_url", "nats://localhost:4222")?
            .add_source(config::Environment::with_prefix("HOTEL").separator("_"))
            .build()?;

        config.try_deserialize()
    }
}